import React, {Component} from 'react'
import {Route} from 'react-router-dom'
import AnimatedRouter from './components/animated_router'
import routes from './routes'
import {post} from './common/js/utils'
import {setUnread} from './actions'

class App extends Component {

  getUnread = () => {
    const {store} = this.props
    post('/dialog/unread').then(res => {
      store.dispatch(setUnread(res.data))
    })
  }

  componentWillMount() {
    this.getUnread()
  }

  shouldComponentUpdate() {
    return true
  }

  render() {
    const fadeRoute = ['/', '/contact', '/user']
    return (
      <AnimatedRouter
        fade={(routePath, lastPath) => fadeRoute.some(item => item === routePath) && fadeRoute.some(item => item === lastPath)}
      >
        {
          routes.map(({name, path, exact = true, component}, key) => (
            <Route path={path} exact={exact} component={component} key={key} />
          ))
        }
      </AnimatedRouter>
    )
  }
}

export default App
